package com.xoa.service.attend;

import com.xoa.dao.attend.AttendLeaveManagerMapper;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.attend.AttendLeaveManager;
import com.xoa.model.department.Department;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.users.Users;
import com.xoa.model.worldnews.News;
import com.xoa.service.department.DepartmentService;
import com.xoa.service.users.UsersService;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AttendLeaveManagerService {

    @Autowired
    private AttendLeaveManagerMapper attendLeaveManagerMapper;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UsersService usersService;
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private DepartmentMapper departmentMapper;

    public ToJson insertSelective (AttendLeaveManager record, HttpServletRequest request){
        ToJson json = new ToJson();
//        List<AttendLeaveManager> record = new ArrayList<>();
//        Map<String,String> map = request.getParameterMap();
//        for (Map.Entry<String, String> vo : map.entrySet()) {
//            String key = vo.getKey();
//            String valu = vo.getValue();
//
//        }


//        Map<String,Map<Object,Object>> param = new HashMap<>();
//        param.put("keys",record);


        try {


//                for (int j = 0; j < record.size(); j++) {
//                    AttendLeaveManager attendLeaveManager = new AttendLeaveManager();
//                    AttendLeaveManager att = record.get(j);
//                    String depId = att.getDeptIdStr();
//                    String userId = att.getManagers();
//                    if (!StringUtils.checkNull(userId)) {
//                        String deptId = usersService.getAllDeptByUserId(userId);
//                        if (depId==deptId){
//                            json.setMsg("添加不符合");
//                            json.setFlag(0);
//                            return json;
//                        }
//                    }
//                }
                int count = attendLeaveManagerMapper.insert(record);
                if (count > 0) {
                    json.setMsg("ok");
                    json.setFlag(0);
                    json.setObject(count);
                }

        }catch (Exception e){
            e.fillInStackTrace();
            json.setMsg("no"+e.getMessage());
            json.setFlag(1);
        }
        return json;
    }


    public ToJson updateByPrimaryKeySelective(AttendLeaveManager record, HttpServletRequest request){
        ToJson json = new ToJson();
        try {
            int count = attendLeaveManagerMapper.updateByPrimaryKeySelective(record);
            if (count>0){
                json.setMsg("ok");
                json.setFlag(0);
                json.setObject(count);
            }
        }catch (Exception e){
            e.fillInStackTrace();
            json.setMsg("no"+e.getMessage());
            json.setFlag(1);
        }
        return json;
    }

    public ToJson deleteByPrimaryKey(Integer managerId, HttpServletRequest request){
        ToJson json = new ToJson();
        try {
            int count = attendLeaveManagerMapper.deleteByPrimaryKey(managerId);
            if (count>0){
                json.setMsg("ok");
                json.setFlag(0);
                json.setObject(count);
            }
        }catch (Exception e){
            e.fillInStackTrace();
            json.setMsg("no"+e.getMessage());
            json.setFlag(1);
        }
        return json;
    }


    public ToJson<AttendLeaveManager> selectAllLeaveManager(Integer page, Integer pageSize, boolean useFlag,HttpServletRequest request){
        ToJson<AttendLeaveManager> json = new ToJson();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", pageParams);
        try {
            List<AttendLeaveManager> list = attendLeaveManagerMapper.selectAllLeaveManager(map);
            if (list.size() > 0) {
                List<AttendLeaveManager> listobj = new ArrayList<>();
                for (int j = 0; j < list.size(); j++) {
                    AttendLeaveManager attendLeaveManager = new AttendLeaveManager();
                    AttendLeaveManager att = list.get(j);
//                    String userid = att.getManagers();
//                    String deptid = att.getDeptIdStr();
                    Integer id = att.getManagerId();
                    attendLeaveManager.setManagerId(id);

                    String depId = att.getDeptIdStr();
                    if (!StringUtils.checkNull(depId)) {
                        String depName = departmentService.getDeptNameByDeptId(depId, ",");
                        if (!"ALL_DEPT".trim().equals(att.getDeptIdStr())) {
                            att.setDeptName(depName + ",");
                        } else {
                            att.setDeptName(depName);
                        }
                        String userId = att.getManagers();
                        if(!StringUtils.checkNull(userId)){
                            String  userName= usersService.getUserNameById(userId);
                            att.setUserName(userName+",");
                        }else {
                            att.setDeptName(depName);
                        }
                    }
                }
                json.setObj(list);
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch (Exception e){
            json.setMsg("no"+e.getMessage());
            json.setFlag(1);
        }
        return json;
    }

    public ToJson<AttendLeaveManager> selectByPrimaryKeyById(Integer managerId){
        ToJson<AttendLeaveManager> json = new ToJson();
        try {
            AttendLeaveManager list = attendLeaveManagerMapper.selectByPrimaryKey(managerId);
            if (list!=null) {
                    String deptId =  list.getDeptIdStr();
                    if (!StringUtils.checkNull(deptId)) {
                    String depName = departmentService.getDeptNameByDeptId(deptId, ",");
                    if (!"ALL_DEPT".trim().equals(list.getDeptIdStr())) {
                        list.setDeptName(depName + ",");
                    } else {
                        list.setDeptName(depName);
                    }
                    String userId = list.getManagers();
                    if(!StringUtils.checkNull(userId)){
                        String  userName= usersService.getUserNameById(userId);
                        list.setUserName(userName+",");
                    }else {
                        list.setDeptName(depName);
                    }
                }
            }
                json.setObject(list);
                json.setMsg("ok");
                json.setFlag(0);

        }catch (Exception e){
            json.setMsg("no");
            json.setFlag(1);
        }
        return json;
    }


}
